package httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/Servlet1","/login/dgut"})
public class Servlet1 extends HttpServlet {
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
        User use = (User) request.getSession().getAttribute("user");
        response.setContentType("text/html;charset=UTF-8");

        if (use == null)
            try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            Jsonb jsonb = JsonbBuilder.create();
            HttpPost httpPost = new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken");
            List<NameValuePair> res = new ArrayList<>();
            res.add(new BasicNameValuePair("token", request.getParameter("token")));
            res.add(new BasicNameValuePair("appid", "javaee"));
            res.add(new BasicNameValuePair("appsecret", "b3b52e43ccfd"));
            res.add(new BasicNameValuePair("uesip", request.getLocalAddr()));

            httpPost.setEntity(new UrlEncodedFormEntity(res));

            try (CloseableHttpResponse response_one = httpClient.execute(httpPost)) {
                HttpEntity entity = response_one.getEntity();
                String str1 = EntityUtils.toString(entity, "UTF-8");
                Text one = jsonb.fromJson(str1, Text.class);
                res.removeAll(res);

                if (one.getError().equals("0")) {
                    httpPost = new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo");
                    res.add(new BasicNameValuePair("openid", one.getOpenid()));
                    res.add(new BasicNameValuePair("access_token", one.getAccess_token()));
                    httpPost.setEntity(new UrlEncodedFormEntity(res));

                    try (CloseableHttpResponse response_two = httpClient.execute(httpPost)) {
                        HttpEntity entity2 = response_two.getEntity();
                        String str2 = EntityUtils.toString(entity2, "UTF-8");
                        User user1 = jsonb.fromJson(str2, User.class);
                        request.getSession().setAttribute("user", user1);
                        request.getSession().setAttribute("token", request.getParameter("token"));
                        response.sendRedirect("http://localhost:8080/mess.jsp");
                    }
                }
                else
                    response.getWriter().println(one.getMessage());
                EntityUtils.consume(entity);
            }

        }
    }
}